import pandas as pd

from datareport.api.annotation.Font import Font
from datareport.api.enum.FontEnum import FontEnum
from datareport.impl.paragraph.base.BaseParagraph import BaseParagraph
from datareport.api.annotation.Style import style
from datareport.api.enum.ParagraphStyleEnum import ParagraphStyleEnum


@style(paragraph=ParagraphStyleEnum.LEVEL_ONE.value)
class KaiZhanQingKuang_31(BaseParagraph):
    """
    开展情况
    """

    def __init__(self):
        super().__init__()
        self.text = '''三、“三会一课”等组织生活开展情况分析'''
        self.fonts=[
            Font(type=FontEnum.LEVEL_ONE.value,text=self.text)
        ]
        self.sort = 21
        self.section = 6

    def plot(self, df, plt):
        pass

    def getData(self, result):
        pass

    def start(self, conn, year, plt):
        # 被观摩
        obed_df=pd.read_sql('''
        select *  from t_meetingplan where isObserve=1 and year(startTime)={}
        '''.format(year),con=conn)
        # 参加观摩
        part_df=pd.read_sql('''
        select ob.*,(select partyBranchName from t_observer where t_observer.observer like concat('%',ob.code,'%')) partyBranchName 
                    from t_observerrecord ob where year(createTime)={}
        '''.format(year),con=conn)
        obed_count=len(obed_df)
        obed_part_count=len(obed_df[obed_df['partyBranchName'].isin(part_df['partyBranchName'].drop_duplicates())])
        # 被观摩2次
        sum_df=obed_df.groupby('partyBranchName')['isObserve'].sum()
        two_count_arr=sum_df[sum_df==2].index.values
        self.text.format(obed_part_count,round(100*obed_part_count/obed_count,1),'、'.join(two_count_arr),len(two_count_arr))

